<!--
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-04 16:35:45
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-05 14:39:59
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <!-- <script src="https://cdn.socket.io/4.4.0/socket.io.min.js"></script> -->
  <script src="./socket.io.min.js"></script>
  <!-- <script src="https://cdn.socket.io/4.4.1/socket.io.esm.min.js"></script> -->
</head>

<body>

  <button id="btn">点击发消息</button>
  <script>
    const socket = io('http://localhost:3009');
    socket.on('connect', () => {
      console.log('看到这里，表示和服务器连接上了')
    })
    // 监听来自服务器的消息
    // message是和服务器约定好的消息频道名字
    socket.on('message', (msg) => {
      console.log('来自服务器', msg)
    })
    document.getElementById('btn').addEventListener('click', () => {
      // 主动给服务器发送消息
      // message是和服务器约定好的消息频道名字
      socket.emit('message', '我是客户端')
    })
  </script>
</body>

</html>